#include <windows.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
     HANDLE console = GetStdHandle(STD_INPUT_HANDLE);
     DWORD mode;
     GetConsoleMode(console, &mode);
     SetConsoleMode(console, mode & ~ENABLE_LINE_INPUT);
     cin >> noskipws;

     srand((int)time(0));

     enum { AC, fortitude, reflex, will };
     
     struct character {

          const char * name;
          int attack_bonus;
          int damage_dice;
          int damage_bonus;
          int defences[4];
          bool is_player;
          int hit_points;
     };

     char answer;

     character Player = { "Player", 5, 10, 10, {20, 15, 15, 20}, true,  20 };
     character Dragon = { "dragon", 8, 10,  5, {20, 17, 17, 15}, false, 20 };

     bool flee = false;
     bool opportunity_attack = false;

     character * attacker = &Player;
     character * defender = &Dragon;

     cout << "A " << defender->name << "!" << endl;
     cout << endl;

     do {

          if (attacker->is_player) {

               do {
                    cout << "Will you:" << endl;
                    cout << "[s] shoot at the " << defender->name << "," << endl;
                    cout << "[r] run away." << endl;
                    cout << endl;
                    cout << ">";

                    cin >> answer;

                    switch (answer) {

                    case 's':
                         cout << endl;
                         cout << endl;
                         cout << "You shoot at the " << defender->name << " with your pistol..." << endl;
                         break;

                    case 'r':
                         cout << endl;
                         cout << "The " << defender->name << " takes a swipe as you flee..." << endl;
                         flee = true;
                         break;

                    default:
                         cout << endl;
                         cout << endl;
                         cout << "I didn't understand that." << endl;
                         cout << endl;
                         answer = '?';
                         break;
                    }

               } while (answer == '?');
          
          } else {
          
               if (flee) {

                    opportunity_attack = true;
               
               } else {

                    cout << "The " << attacker->name << " attacks you..." << endl;
               }
          }
          
          if (opportunity_attack || !flee) {

               int attack_bonus = attacker->attack_bonus + (opportunity_attack? 2 : 0);
               int defence = defender->defences[AC] - (opportunity_attack? 5 : 0);
               if (rand() % 20 + 1 + attack_bonus >= defence) {

                    int damage = rand() % attacker->damage_dice + 1 + attacker->damage_bonus;
                    cout << "a hit for " << damage << " points of damage!" << endl;
                    defender->hit_points -= damage;
               
               } else {

                    cout << "miss!" << endl;
               }
          }
          
          character * temporary = attacker;
          attacker = defender;
          defender = temporary;

     } while (attacker->hit_points > 0 && defender->hit_points > 0 && !opportunity_attack);

     if (Player.hit_points < 1) {

          cout << "You died!" << endl;

     } else if (Dragon.hit_points < 1) {

          cout << "You defeated the dragon!" << endl;

     } else {

          cout << "You live to fight another day..." << endl;
     }

     cin >> answer; // wait for keypress to end

     return 0;
}
